import re
from rest_framework.views import APIView
from django.core.cache import cache
from django.conf import settings

from utils.response import APIResponse
from libs import tx_sms
from . import serializers, models, throttles


# 多方式登录
class LoginAPIView(APIView):
    authentication_classes = []
    permission_classes = []

    def post(self, request, *args, **kwargs):
        serializer = serializers.LoginModelSerializer(data=request.data)
        if serializer.is_valid():
            return APIResponse(data={
                'username': serializer.user.username,
                'token': serializer.token
            })
        return APIResponse(1, 'failed', data=serializer.errors, http_status=400)


# 手机验证码登录
class LoginMobileAPIView(APIView):
    authentication_classes = []
    permission_classes = []

    def post(self, request, *args, **kwargs):
        serializer = serializers.LoginMobileModelSerializer(data=request.data)
        if serializer.is_valid():
            return APIResponse(data={
                'username': serializer.user.username,
                'token': serializer.token
            })
        return APIResponse(1, 'failed', data=serializer.errors, http_status=400)


# 手机验证码注册
class RegisterAPIView(APIView):
    authentication_classes = []
    permission_classes = []

    def post(self, request, *args, **kwargs):
        serializer = serializers.RegisterMobileSerializer(data=request.data)
        if serializer.is_valid():
            obj = serializer.save()
            return APIResponse(0, '注册成功', data={
                'username': obj.username,
                'mobile': obj.mobile,
                'email': obj.email,
            })
        return APIResponse(1, '注册失败', data=serializer.errors, http_status=400)


# 发送短信
class SMSAPIView(APIView):
    throttle_classes = [throttles.SMSRateThrottle]

    def post(self, request, *args, **kwargs):
        mobile = request.data.get('mobile')

        if not (mobile and re.match(r'^1[3-9][0-9]{9}$', mobile)):
            return APIResponse(2, '手机号格式有误')

        # 获取验证码
        code = tx_sms.get_code()

        # 发送短信
        result = tx_sms.send_msg(mobile, code, settings.SMS_EXP // 60)

        if not result:
            return APIResponse(1, '发送验证码失败')

        cache.set(settings.SMS_CACHE_KEY % mobile, code, settings.SMS_EXP)

        return APIResponse(0, '发送验证码成功')


# 手机注册验证
class MobileAPIView(APIView):
    def post(self, request, *args, **kwargs):
        mobile = request.data.get('mobile')

        if not (mobile and re.match(r'^1[3-9][0-9]{9}$', mobile)):
            return APIResponse(2, '手机号格式有误', http_status=400)

        try:
            models.User.objects.get(mobile=mobile)
            return APIResponse(1, '手机已注册')
        except:
            return APIResponse(0, '手机未注册')
